package com.translate.web.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import com.translate.web.constant.RedisConstant;
import com.translate.web.dao.CustomerLabelMapper;
import com.translate.entity.Customer;
import com.translate.entity.CustomerLabel;
import com.translate.web.service.ICustomerLabelService;
import com.basic.service.impl.BaseServiceImpl;
import com.translate.vo.contract.ICustomerLabelVO;

@Service
public class CustomerLabelServiceImpl extends BaseServiceImpl<CustomerLabel> implements ICustomerLabelService {

	public CustomerLabelServiceImpl(CustomerLabelMapper mapper) {
		super(mapper);
		// TODO Auto-generated constructor stub
	}

	private RedisTemplate<String, Object> redisTemplate;

	@Autowired
	public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	@Override
	protected void insertPropteris(CustomerLabel data) {

		long time = System.currentTimeMillis() / 1000;

		data.setCreateTime(time);

		data.setUpdateTime(time);
	}

	@Override
	protected Class<CustomerLabel> getEntityClass() {
		// TODO Auto-generated method stub
		return CustomerLabel.class;
	}

	@Override
	protected CustomerLabel getEntityInstance() {
		// TODO Auto-generated method stub
		return new CustomerLabel();
	}

	@Override
	protected void updatePropteris(CustomerLabel data) {
		// TODO Auto-generated method stub
		data.setUpdateTime(System.currentTimeMillis() / 1000);

	}

	protected int getId(Customer data) {
		return data.getId();
	}

	@Override
	public <VO extends ICustomerLabelVO> List<ICustomerLabelVO> getCustomerLabelsToCache(Class<VO> target) {

		List<CustomerLabel> customerLabels = this.getCustomerLabelsInCache();

		ICustomerLabelVO customerLabelVO;

		List<ICustomerLabelVO> customerLabelVOs = new ArrayList<>(32);

		try {
			for (CustomerLabel customerLabel : customerLabels) {

				customerLabelVO = target.newInstance();

				customerLabelVO.setId(customerLabel.getId());

				customerLabelVO.setLabelName(customerLabel.getLabelName());

				customerLabelVO.setpId(customerLabel.getpId());

				customerLabelVO.setSort(customerLabel.getSort());
				
				customerLabelVOs.add(customerLabelVO);

			}
		} catch (Exception e) {
			throw new Error(e);
		}

		
		return customerLabelVOs;
	}

	public List<CustomerLabel> getCustomerLabelsInCache() {
		String key = RedisConstant.getCustomerLabelKey();

		ValueOperations<String, Object> valueOperations = this.redisTemplate.opsForValue();

		List<CustomerLabel> departments = (List<CustomerLabel>) valueOperations.get(key);

		if (null != departments) {
			return departments;
		}

		CustomerLabelMapper departmentMapper = this.getBaseMapper();

		departments = departmentMapper.getCustomerLabelByStatus();

		if (null == departments) {
			departments = new ArrayList<>(0);
			valueOperations.set(key, departments, 3, TimeUnit.SECONDS);

			return departments;
		}

		departments.sort((f,s)-> s.getSort() - f.getSort()) ;

		valueOperations.set(key, departments, 20, TimeUnit.SECONDS);

		return departments;
	}

}